package com.mingqijia.gassafety.console.controller;

import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.shared.constant.OperateRecordEnum;
import com.mingqijia.gassafety.shared.log.LogRecord;
import com.mingqijia.gassafety.webserver.request.*;
import com.mingqijia.gassafety.webserver.response.*;
import com.mingqijia.gassafety.webserver.service.FunctionAreaService;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;

/**
 * 功能区域
 * @Author: duanhongxiang
 * @Date: 2024/6/12 16:54
 */
@Slf4j
@RestController
@Api(tags = "功能区域")
@RequestMapping("/business/v1/funcarea")
public class FunctionAreaController {
    @Autowired
    FunctionAreaService functionAreaService;

    @ApiOperation(value = "功能区域-区,街道树形列表")
    @GetMapping("/trees")
    public CommonResponse<List<FuncAreaTreeResponse>> treeList() {
        List<FuncAreaTreeResponse> result = functionAreaService.treeList();
        return CommonResponse.success(result);
    }

    @ApiOperation(value = "功能区域-商圈列表")
    @GetMapping("/business/area")
    public CommonResponse<List<BusinessAreaItemResponse>> businessAreaList(BusinessAreaListRequest request) {
        List<BusinessAreaItemResponse> result = functionAreaService.businessAreaList(request);
        return CommonResponse.success(result);
    }

    @ApiOperation(value = "功能区域-区,街道，商圈-树形列表")
    @GetMapping("/list")
    public CommonResponse<List<FuncAreaListResponse>> List(FuncAreaQueryRequest request) {
        List<FuncAreaListResponse> result = functionAreaService.List(request);
        return CommonResponse.success(result);
    }

    @ApiOperation(value = "功能区域管理-父级区域-下拉框(包含功能区和街道)")
    @GetMapping("/selectList")
    public CommonResponse<List<FuncAreaDetailResponse>> selectList(@RequestParam(required = false)  @ApiParam(value = "层级(不传时，查询功能区+街道)") Integer level) {
        return CommonResponse.success(functionAreaService.selectList(level));
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_73, detail = "'新增【' + #req.funcName + '】'", commonInfo ="#req.funcName")
    @ApiOperation(value = "功能区域-新增")
    @PostMapping("/add")
    public CommonResponse<Boolean> add(@Valid  @RequestBody FuncAreaAddRequest req) {
        if (null == req.getPid()) req.setPid(0);
        return functionAreaService.add(req);
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_74)
    @ApiOperation(value = "功能区域-编辑")
    @PostMapping("/edit")
    public CommonResponse<Boolean> edit(@Valid @RequestBody FuncAreaEditRequest req) {
        if (null == req.getPid()) req.setPid(0);
        return functionAreaService.edit(req);
    }

    @ApiOperation(value = "功能区域-详情")
    @GetMapping("/detail")
    public CommonResponse<FuncAreaDetailResponse> detail(@RequestParam
                                              @ApiParam(value = "id", required = true) Integer id) {
        log.info("id:{}", id);
        if (null == id) {
            return CommonResponse.failure("id不能为空");
        }
        return functionAreaService.detail(id);
    }

    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_75)
    @ApiOperation(value = "功能区域-删除")
    @PostMapping("/delete")
    public CommonResponse<Boolean> delete(@Valid @RequestBody FuncAreaIdRequest req) {
        return functionAreaService.delete(req.getId());
    }

    @ApiOperation(value = "功能区域-关联数量信息")
    @GetMapping("/relationInfo")
    public CommonResponse<FuncAreaRelationInfoResponse> relationInfo(@RequestParam
                                              @ApiParam(value = "id", required = true) Integer id) {
        return functionAreaService.relationInfo(id);
    }

    @ApiOperation(value = "功能区域-父级围栏信息")
    @GetMapping("/parentAndPeerInfo")
    public CommonResponse<FuncAreaParentAndPeerInfoResponse> parentAndPeerInfo(@RequestParam(value = "pid",required = false)
                                                         @ApiParam(value = "pid") Integer pid) {
        log.info("pid:{}", pid);
        return functionAreaService.parentAndPeerInfo(pid);
    }

    @ApiOperation(value = "功能区域-同级围栏信息+最大顺序值")
    @GetMapping("/maxOrdinal")
    public CommonResponse<FuncAreaPeerInfoListResponse> maxOrdinal(@RequestParam(value = "level",required = true)
                                               @ApiParam(value = "level",required = true) Integer level) {
        return functionAreaService.maxOrdinal(level);
    }

    @ApiOperation(value = "功能区域-所能选的层级")
    @GetMapping("/level")
    public CommonResponse<List<FuncAreaLevelResponse>> getSelectLevel() {
        List<FuncAreaLevelResponse> result = functionAreaService.getLevels();
        return CommonResponse.success(result);
    }
}
